

package com.wc.service.impl;

import com.wc.dao.AuthorizeMapper;
import com.wc.dao.ProjectMapper;
import com.wc.exception.CustomException;
import com.wc.pojo.Project;
import com.wc.service.ProjectService;
import com.wc.tree.TreeNode;
import com.wc.util.Information;
import com.wc.util.StringUtils;
import com.wc.util.TreeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目实现类
 */
@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper mapper;
    @Autowired
    private AuthorizeMapper authorizeMapper;

    protected static Logger logger = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Override
    public List<Project> getShow() {
        return mapper.getProjectList();
    }

    @Override
    public boolean removeProject(String bmDm) {
        boolean result = false;
        try {
            //判空
            if (StringUtils.isEmpty(bmDm)) throw CustomException.DM_IS_NULL;
            Project project = mapper.getBmdm(bmDm);
            //查看明细字段来判断当前节点是否有子节点
            if (project.getXmDetailed() == 1) throw CustomException.HAVE_CHILD_NODE;
            //当节点删除的时候需要将授权表中对应的授权ID进行删除
            int rows = authorizeMapper.deleteAuthorize(project.getXmOwnerid());
            if (rows > 0){
                logger.info(Information.SQID_SUCCESS);
            }else {
                logger.info(Information.SQID_FAIL);
            }
            //当节点删除 它对应的父级节点的明细要设置为0
            int number = mapper.updateVariable(0, project.getXmDm());
            if (number > 0) {
                logger.info(Information.DETAILED_SUCCESS);
            } else {
                logger.info(Information.DETAILED_FAIL);
            }
            result = mapper.removeProject(bmDm) > 0 ? true : false;
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return result;
    }

    @Override
    public boolean addProject(Project project) {
        boolean result = false;
        try {
            //判空
            if (StringUtils.isEmpty(project.getXmName()) || StringUtils.isEmpty(project.getXmDm()) || StringUtils.isEmpty(project.getXmJea()) || StringUtils.isEmpty(project.getXmJezb()) || StringUtils.isEmpty(project.getXmJejc()) || StringUtils.isEmpty(project.getXmOwnerid()) || StringUtils.isEmpty(project.getXmTime())) throw CustomException.CANNOT_BE_EMPTY;
            //计算有效金额,并赋值
            if (StringUtils.isEmpty(project.getXmJeyx())) project.setXmJeyx(computeYxje(project.getXmJea(),project.getXmJezb(),project.getXmJejc()));
            //设置时间
            if (StringUtils.isEmpty(project.getXmOldtime()))  project.setXmOldtime(new Date());
            //由于直接使用父级id查不到，所以都需要截取2位才能查到
            String oldXmdm = project.getXmDm().substring(0,project.getXmDm().length()-2);
            //将当前添加的节点的级次设置为父节点级次加一就是子节点的级次
            if (StringUtils.isEmpty(project.getBxLevel()))  project.setBxLevel(mapper.parentLevel(oldXmdm)+1);
            Project projectOld = getOwnerid(project.getXmOwnerid());
            if (!StringUtils.isEmpty(projectOld)) throw CustomException.XMOWNERID_EXISTS;
            //将当前节点明细设置为0
            project.setXmDetailed(0);
            //将当前对应的父节点的明细修改为1
            int number = mapper.updateVariable(1, project.getXmDm());
            if (number > 0) {
                logger.info(Information.DETAILED_SUCCESS);
            } else {
                logger.info(Information.DETAILED_FAIL);
            }
            result = mapper.addProject(project) > 0 ? true : false;
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return result;
    }

    @Override
    public Project getBmdm(String bmDm) {
        try {
            if (StringUtils.isEmpty(bmDm)) throw CustomException.DM_IS_NULL;
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return mapper.getBmdm(bmDm);
    }

    @Override
    public Project getAuthorize(Integer xmOwnerid) {
        try {
            if (StringUtils.isEmpty(xmOwnerid)) throw CustomException.SQID_IS_NULL;
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return mapper.getOwnerid(xmOwnerid);
    }

    @Override
    public Project getOwnerid(Integer xmOwnerid) {
        try {
            if (StringUtils.isEmpty(xmOwnerid)) throw CustomException.XMOWNERID_IS_NULL;
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return mapper.getOwnerid(xmOwnerid);
    }

    @Override
    public List<Project> getChildren(String xmDm) {
        try {
            if (StringUtils.isEmpty(xmDm)) throw CustomException.DM_IS_NULL;
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return mapper.getChildren(xmDm);
    }

    @Override
    public List<Project> getTreeType() {
        return mapper.getProjectList();
    }

    @Override
    @Transactional
    public boolean updateProject(Project project) {
        boolean result = false;
        try {
            if (StringUtils.isEmpty(project.getXmId()) || StringUtils.isEmpty(project.getXmName()) || StringUtils.isEmpty(project.getXmJea()) || StringUtils.isEmpty(project.getXmJezb()) || StringUtils.isEmpty(project.getXmJejc()) ||  StringUtils.isEmpty(project.getXmOldtime()))  throw CustomException.CANNOT_BE_EMPTY;
            //计算有效金额,并赋值
            if (StringUtils.isEmpty(project.getXmJeyx())) project.setXmJeyx(computeYxje(project.getXmJea(),project.getXmJezb(),project.getXmJejc()));
            result = mapper.updateProject(project) > 0 ? true : false;
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return result;
    }

    @Override
    public List<TreeNode> getTree() {
        //待处理数据集合
        List<Project> projectList = mapper.getProjectList();
        //预保存树结构集合
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Project project : projectList) {
            TreeNode treeNode = new TreeNode();
            //将查出的数据和树形结构的属性对应赋值
            treeNode.setId(project.getBmDm());
            treeNode.setParentId(project.getXmDm());
            if (project.getBmDm().length() == 3) {
                treeNode.setName(project.getBmName());
            }
            if (project.getBmDm().length() > 3) {
                treeNode.setName(project.getXmName());
            }
            //赋值好的树结构对象保存到树结构集合中
            treeNodes.add(treeNode);
        }
        //通过工具类处理成树形结构集合
        treeNodes = TreeUtil.build(treeNodes, "0");
        return treeNodes;
    }

    /**
     * 计算有效金额方法
     * @param jea
     * @param jezb
     * @param jejc
     * @return
     */
    private Double computeYxje(Double jea,Double jezb,Double jejc){
        return jea + jezb - jejc;
    }

}


